package com.example.seleniumtest;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class example2 {
    static WebDriver webDriver=null;

    public static void createDriver() throws InterruptedException {
        //驱动程序自动化
        // 设置本地 ChromeDriver 路径
        System.setProperty("webdriver.chrome.driver", "C:\\学习使我快乐\\测试\\Selenium-Test\\chromedriver-win64\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();

        //允许访问所有连接
        options.addArguments("--remote-allow-origins=*");

        //设置完基础设置之后,就开始进行操作
        //1.打开对应的浏览器(首先要创建一个驱动对象)
        ChromeDriver webDriver = new ChromeDriver(options);
        Thread.sleep(3000);
        //2.输入对应网址,这里访问百度
        webDriver.get("https://www.baidu.com/");
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        test5();
        Thread.sleep(5000);
        //5.关闭浏览器
        webDriver.quit();
    }

    //等待
    public static void test1() throws InterruptedException {
        createDriver();
        //webDriver.findElement(By.cssSelector("#sanRoot > main > div.hot-wrap_1nNog > div.theme-hot.category-item_1fzJW > div.title-wrap_256Xx > a.more_Uf8LD > span"));
//        webDriver.manage().timeouts().implicitlyWait(Duration.ofMillis(2000));
//        webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));

        //显示等待
        //new WebDriverWait(webDriver,Duration.ofSeconds(3)).until($express);//三秒内 until 内的条件没有达成,就会报错

        //分开写
        WebDriverWait webDriverWait = new WebDriverWait(webDriver,Duration.ofSeconds(3));
        //webDriverWait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#su")));//保证元素可以进行点击

        //webDriver.findElement(By.cssSelector("#su")).click();
        webDriverWait.until(ExpectedConditions.textToBe(By.cssSelector("#hotsearch-refresh-btn > span"),"换一换1"));

    }

    public static void test2() throws InterruptedException {
        createDriver();

        //先跳到后一个页面
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("CSDN");
        webDriver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);
        webDriver.navigate().back();//后退
        Thread.sleep(3000);
        webDriver.navigate().forward();//前进
        Thread.sleep(3000);
        webDriver.navigate().refresh();//刷新

    }

    //提示窗
    public static void test3() throws InterruptedException {
        createDriver();

        webDriver.findElement(By.cssSelector("body > div:nth-child(3)")).click();
        Thread.sleep(3000);
//        Alert alert = webDriver.switchTo().alert();
//        //确认
//        alert.accept();
//        //取消
//        alert.dismiss();


        Alert alert = webDriver.switchTo().alert();
        alert.sendKeys("111");
        //确认
        alert.accept();
        //取消
        alert.dismiss();
    }

    //测试等待对结果的影响
    public static void test5() throws InterruptedException {
        createDriver();
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("重庆邮电大学");
        webDriver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);

        //找这个
        webDriver.findElement(By.cssSelector("#\\31  > div > div > div._content_zc167_4 > div.cu-mt-sm-lg.cu-mt-xs-md.paragraph_5KpHg > div > div > p > span:nth-child(1) > span"));
    }

    //浏览器参数设置
    public static void test4() throws InterruptedException {
        createDriver();

    }
}
